<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="前面的视图都是使用代码创建，QT也支持可视化编程，且性能强大。只需要在创建项目时勾选“创建界面”（默认勾选），生成的项目中就会自动生成支持可视化编程的.ui文件。
点击左侧“设计”按钮，.ui的样式如下：
控件区
Layouts 布局区，少用

Spacers 填补布局时的间隙，缩放界面时自适应（详">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="QT基础4——可视化编程">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="前面的视图都是使用代码创建，QT也支持可视化编程，且性能强大。只需要在创建项目时勾选“创建界面”（默认勾选），生成的项目中就会自动生成支持可视化编程的.ui文件。
点击左侧“设计”按钮，.ui的样式如下：
控件区
Layouts 布局区，少用

Spacers 填补布局时的间隙，缩放界面时自适应（详">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>QT基础4——可视化编程 - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>QT基础4——可视化编程</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2018-12-16
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        

<a href="/blog/categories/QT/">QT</a>

                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>前面的视图都是使用代码创建，QT也支持可视化编程，且性能强大。只需要在创建项目时勾选“创建界面”（默认勾选），生成的项目中就会自动生成支持可视化编程的.ui文件。</p>
<p>点击左侧“设计”按钮，.ui的样式如下：<br><img src="http://cyrus_huang.gitee.io/blog/2018/12/16/QT基础4——可视化编程/design_all.png" alt=""></p>
<h3 id="控件区"><a href="#控件区" class="headerlink" title="控件区"></a>控件区</h3><ul>
<li><p>Layouts 布局区，少用</p>
</li>
<li><p>Spacers 填补布局时的间隙，缩放界面时自适应（详看上图）</p>
</li>
</ul>
<p>1、Horizontal Spacer 可以填补水平间隙</p>
<p>2、Vertical Spacer    可以填补垂直间隙</p>
<ul>
<li>Buttons</li>
</ul>
<p>1、Push Button    常用按钮</p>
<p>2、Tool Button </p>
<p>3、Radio Button 单选按钮，必须处于同一父视图同一层级</p>
<p>4、Check Box    多选按钮</p>
<ul>
<li><p>Item Views 和 Item Widgets</p>
</li>
<li><p>Containers 各种可以作为父视图的控件</p>
</li>
<li><p>Input Widgets 各种输入控件</p>
</li>
<li><p>Display Widgets 各种展示类控件</p>
</li>
</ul>
<p>示例<br><img src="http://cyrus_huang.gitee.io/blog/2018/12/16/QT基础4——可视化编程/design_add.png" alt=""> <img src="http://cyrus_huang.gitee.io/blog/2018/12/16/QT基础4——可视化编程/design_result.png" alt=""></p>
<h3 id="视图层级区"><a href="#视图层级区" class="headerlink" title="视图层级区"></a>视图层级区</h3><p>可以查看当前窗口的所有视图及相应的层级分布，layout状态。可以修改相应控件的名称及进行其他操作。</p>
<h3 id="控件属性区"><a href="#控件属性区" class="headerlink" title="控件属性区"></a>控件属性区</h3><p>属性区由控件的继承关系从上到下展示控件的可编辑属性，如控件名称，控件大小，是否可拉伸等等。</p>
<h3 id="信号和槽区"><a href="#信号和槽区" class="headerlink" title="信号和槽区"></a>信号和槽区</h3><p>信号和槽区可以对窗口控件直接添可信号和相当的槽响应，无需编写Connect函数。</p>
<h3 id="代码调用-ui文件创建的控件"><a href="#代码调用-ui文件创建的控件" class="headerlink" title="代码调用.ui文件创建的控件"></a>代码调用.ui文件创建的控件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">MainWindow::MainWindow(QWidget *parent) :</span><br><span class="line">    QMainWindow(parent),</span><br><span class="line">    ui(new Ui::MainWindow)</span><br><span class="line">&#123;</span><br><span class="line">	//ui文件初始化，勾选“创建界面”后系统自动生成</span><br><span class="line">    ui-&gt;setupUi(this);</span><br><span class="line"></span><br><span class="line">	//ui-&gt;控件名，可以选取相应控件的指针，控件名可以在“视图层级区”查看及修改</span><br><span class="line">    ui-&gt;myButton-&gt;setText(&quot;我了个去&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="创建按钮槽事件"><a href="#创建按钮槽事件" class="headerlink" title="创建按钮槽事件"></a>创建按钮槽事件</h3><p>选择相应的按钮（如myButton)，右键，转到槽，选择相应的按钮信号（如released()），就会自动生成槽函数 void on_按钮名_信号名（）。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">.h文件</span><br><span class="line">private slots:</span><br><span class="line">    void on_myButton_released();</span><br><span class="line">    </span><br><span class="line">.cpp文件</span><br><span class="line">void MainWindow::on_myButton_released()</span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>也可以创建好相应的槽函数后，在信号与槽编辑区直接添加绑定，不用调用Connect函数<br><img src="http://cyrus_huang.gitee.io/blog/2018/12/16/QT基础4——可视化编程/design_slot.png" alt=""></p>
<h3 id="在ui界面中使用自定义类"><a href="#在ui界面中使用自定义类" class="headerlink" title="在ui界面中使用自定义类"></a>在ui界面中使用自定义类</h3><ul>
<li>1、创建自定义类子类，如QPushButton子类 MyButton</li>
<li>2、选中一个QPushButton，右键-&gt;提升为，填写“提升的类名称”-&gt;MyButton， 勾选“全局包含”， 点击“添加”, 再点击“提升”即可。</li>
</ul>
<p>将自定义降为父类：选中提升后的控件，右键选择“选择xxx的提升”。</p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>